package com.hele.sts.dao;

import java.io.PrintStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hele.sts.pub.ConnectDB;
import com.hele.sts.pub.SpringContextHolder;

public class DjjgDao
{
  public static List loadDjjg()
  {
    Connection conn = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    String sql = null;
    ConnectDB connsyb = null;
    String content = null;
    List list = new ArrayList();
    sql = "select code,content from bm_djjg where code in (select djjg from xt_server)";
    try
    {
      connsyb = SpringContextHolder.getBean(ConnectDB.class);
      conn = connsyb.getConnection();
      pstmt = conn.prepareStatement(sql);
      rs = pstmt.executeQuery();
      while (rs.next()) {
        list.add(rsToBean(rs));
      }
      return list;
    }
    catch (SQLException e)
    {
      logger.info("db error", e);
    }
    finally
    {
      DbClose.freePreparedStatement(pstmt);
      DbClose.freeConnection(conn);
    }
   
    return null;
  }


  public static List<Bm> getDjjgList()
  {
    Connection conn = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    String sql = null;
    ConnectDB connsyb = null;
    String content = null;
    List<Bm> list = new ArrayList();
    sql = "select code,content from bm_djjg where code in (select djjg from xt_server)";
    try
    {
      connsyb = SpringContextHolder.getBean(ConnectDB.class);
      conn = connsyb.getConnection();
      pstmt = conn.prepareStatement(sql);
      rs = pstmt.executeQuery();
      while (rs.next()) {
        Bm bm = new Bm();
        bm.setCode(rs.getString("code"));
        bm.setContent(rs.getString("content"));
        list.add(bm);
      }
      return list;
    }
    catch (SQLException e)
    {
      logger.info("db error", e);
    }
    finally
    {
      DbClose.freePreparedStatement(pstmt);
      DbClose.freeConnection(conn);
    }

    return null;
  }
  
  public static List djjginfo()
  {
    ConnectDB connsyb = SpringContextHolder.getBean(ConnectDB.class);
    String sql = "select code,content from bm_djjg where code in (select djjg from xt_server)";
   
    
    List list = new ArrayList();
    try
    {
    	 Connection conn = connsyb.getConnection();
      PreparedStatement pst = conn.prepareStatement(sql);
      ResultSet rs = pst.executeQuery();
      while (rs.next())
      {
        list.add(rsToBean(rs));
//        System.out.println(rs.getString(1));
//        System.out.println(rs.getString(2));
      }
      return list;
    }
    catch (SQLException e)
    {
      e.printStackTrace();
    }
    return null;
  }
  
  static XtServer rsToBean(ResultSet rs)
    throws SQLException
  {
    XtServer bean = new XtServer();
    bean.setCode(rs.getString("code"));
    bean.setContent(rs.getString("content"));
    return bean;
  }
  
  public static int count1(String djjg)
  {
    Connection conn = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    String sql = null;
    ConnectDB connsyb = null;
    sql = "SELECT count(*) FROM xt_server WHERE 1=1 ";
    if ((djjg != null) && (djjg.length() > 0)) {
      sql = sql + 
        " AND djjg = ? ";
    }
    logger.debug(sql);
    int j = 0;
    try
    {
      connsyb = SpringContextHolder.getBean(ConnectDB.class);
      conn = connsyb.getConnection();
      pstmt = conn.prepareStatement(sql);
      int i = 1;
      if ((djjg != null) && (djjg.length() > 0)) {
        pstmt.setString(i++, 
          "%" + djjg + "%");
      }
      rs = pstmt.executeQuery();
      if (!rs.next()) {
        j = rs.getInt(1);
      }
      return j;
    }
    catch (SQLException e)
    {
      logger.info("db error", e);
    }
    finally
    {
      DbClose.freePreparedStatement(pstmt);
      DbClose.freeConnection(conn);
      DbClose.freeResultSet(rs);
    }
    DbClose.freePreparedStatement(pstmt);
    DbClose.freeConnection(conn);
    DbClose.freeResultSet(rs);
    

    return 0;
  }
  
  static Logger logger = LoggerFactory.getLogger(DjjgDao.class);
  public static final String TABLE1 = "xt_server";
}
